<?php

/**
 * @author Watanabex
 All the database functions
 * @copyright 2011
 */

include('classes/utils/DB.php');

class DBFunctions{
	private static $instance = null;
	
	public function getInstance(){		
		if(!self::$instance){
			self::$instance = new DBFunctions();
		}
		return self::$instance;
	}
		
	public static function sayHello($var){
		echo "<br>say hello .".$var;
	}
	
	/*Displays the query with the prepared Statements
        *  @param string SQL statement, array parameters
        *  @returns nothing it just prints the query
        */
        public static function displayQ($sSql, $arrParams = array()){
                $sSql = vsprintf($sSql, $arrParams);
                print_r($sSql);
                die();
        }
    
    /*Executes queries used to perform select type queries
        *  @param string SQL statement, array parameters
        *  @returns array with query results
        */
        public static function execSelect($sSql, $arrParams = array()){
            $sSql = vsprintf($sSql, $arrParams);

            $oDB = DB::getInstance();

            $result = $oDB->query($sSql) or exit("Error code ({$oDB->errno}): {$oDB->error}");
            $rows =array();
            while($row = $result->fetch_assoc()) {
                $rows[] = $row;
            }

            return $rows;
        }
    
    /*Executes INSERT, UPDATE and DELETE type of queries
        *  @param string SQL statement, array parameters
        *  @returns int number of affected rows
        */
        public static function execQ($sSql, $arrParams = array()){
                $sSql = vsprintf($sSql, $arrParams);

                $oDB = DB::getInstance();

                $result = $oDB->query($sSql) or exit("Error code ({$oDB->errno}): {$oDB->error}");
                $count = $oDB->affected_rows;

                return $count;
        }

    /*Gets the last Id that was inserted
        *  @param none
        *  @returns int last inserted id
        */
        public static function getLastID(){
                //              return lastid
                $oDB = DB::getInstance();

                return $oDB->insert_id;
        }

    public static function destroyInstance(){
        $oDB = DB::destroyInstance();
    }
}